<!-- 头部 -->
<template>
  <div class="header-container">
    <div class="l-content">
      <router-link to="/home/homePage">
        <el-button @click="changeMenu" icon="el-icon-menu" size="mini">
        </el-button>
      </router-link>
    </div>
    <el-badge :value="notReadMsgNum" :max="1000" class="item">
      <span class="icon" @click="msgClick">
        <i class="el-icon-bell"></i>
      </span>
    </el-badge>
    <div class="r-content">
      <el-dropdown @command="handleCommand" class="head">
        <span class="el-dropdown-link">
          <el-avatar
            class="user_head"
            v-if="userData.headPic === null || userData.headPic === ''"
            icon="el-icon-user-solid"
          ></el-avatar>
          <img v-else :src="userData.headPic" class="user_head" />
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="personalCenter">个人中心</el-dropdown-item>
          <el-dropdown-item command="exit">退出</el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { notReadCount } from "@/services/msg";
import { loginOut } from "@/services/login";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  computed: {},
  data() {
    //这里存放数据
    return {
      userData: {},
      notReadMsgNum: 0,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    msgClick() {
      if (this.$route.path !== "/home/systemMess") {
        this.$router.push("/home/systemMess").catch((error) => {});
      }
    },
    getNotReadCount() {
      notReadCount()
        .then((res) => {
          this.notReadMsgNum = res.data;
        })
        .catch((error) => {});
    },

    changeMenu() {},
    handleCommand(handleCommand) {
      if (handleCommand === "exit") {
        loginOut()
          .then((res) => {
            sessionStorage.removeItem("Authorization");
            this.$router.push("/login");
          })
          .catch((error) => {});
      } else if (handleCommand === "personalCenter") {
        if (this.$route.path !== "/home/personalCenter")
          this.$router.push("/home/personalCenter").catch((error) => {});
      }
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.userData = JSON.parse(sessionStorage.getItem("userData"));
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getNotReadCount();
    this.$bus.$on("message-change", this.getNotReadCount);
  },
  beforeDestroy() {
    //当前组件被销毁前，消息总线必须要被收到销毁
    this.$bus.$off(["message-change"]);
  },
};
</script>
<style lang="scss" scoped>
.header-container {
  height: 60px;
  background-color: #fffefe;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0 20px;
  border-left: 1px solid #e5e7eb;
  border-right: 1px solid #e5e7eb;
  border-bottom: 2px solid #e5e7eb;
}

.span_text {
  display: flex;

  .el-breadcrumb {
    .el-breadcrumb-item {
      font-size: 14px;
      color: #fff;
      margin-left: 10px;
      align-items: center;
      justify-items: center;
    }
  }
}

.l-content {
  display: flex;
  align-items: center;

  .span_text {
    margin-left: 20px;

    /deep/.el-breadcrumb__item {
      .el-breadcrumb__inner {
        font-weight: normal;
        color: #fff;
      }
    }
  }
}

.item {
  margin-left: 85%;

  .icon {
    display: flex;
    font-size: 25px;
  }
}
.item:hover {
  color: rgb(57, 149, 255);
}
.user_head {
  margin-left: 40px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
</style>